//
//  LoginVC.m
//  jzyStudents
//
//  Created by 任辰东 on 2022/9/21.
//

#import "LoginVC.h"
#import "RCDTextFeidView.h"
#import "UIButton+LXMImagePosition.h"
#import "UIButton+TouchAreaInsets.h"
#import "RCDCornerBtn.h"
#import "AgreementAndPolicyView.h"
#import "RCDWebViewController.h"
#import "LoginApis.h"
#import "NSString+Size.h"
#import "UIButton+CountDown.h"
#import "UIView+RCDToast.h"
#import "UIImage+color.h"
#import "LoginWebViewController.h"

@interface LoginVC ()

@property (nonatomic, strong) UILabel *tipsLabel;
@property (nonatomic, strong) UILabel *tipsLabel1;
@property (nonatomic, strong) UIView *mobileBgView;
@property (nonatomic, strong) RCDTextFeidView *mobileView;

@property (nonatomic, strong) UIView *pswBgView;
@property (nonatomic, strong) RCDTextFeidView *pswView;
@property (nonatomic, strong) UIButton *pswBtn;

@property (nonatomic, strong) UIButton *forgetBtn;

@property (nonatomic, strong) UILabel *markLabel;
@property (nonatomic, strong) AgreementAndPolicyView *policyView;
@property (nonatomic, strong) RCDCornerBtn *loginBtn;

@property (nonatomic, assign) BOOL isSelectedPrivacy;
@property (nonatomic, assign) BOOL isSelectedPrivacyAcctont;
@end

@implementation LoginVC


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
    [self.navigationController setNavigationBarHidden:NO];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"登录";
    
    self.isSelectedPrivacy = NO;
    self.isSelectedPrivacyAcctont = NO;

    WeakSelf
    [self.policyView setDidPrivacyPolicy:^{
            StrongSelf
        LoginWebViewController *webVC = [[LoginWebViewController alloc]initWithUrl:kPrivacyPolicyUrl];
        webVC.navigationItem.title = @"隐私政策";
        RCDBaseNavigationController *nav = [[RCDBaseNavigationController alloc]initWithRootViewController:webVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:NO completion:NULL];
        
//        RCDWebViewController *webVC = [[RCDWebViewController alloc]initWithUrl:kPolicyUrl];
//        webVC.navigationItem.title = @"隐私政策";
//        [self.navigationController pushViewController:webVC animated:YES];
    }];
    
    [self.policyView setDidUserAgreement:^{
            StrongSelf
        LoginWebViewController *webVC = [[LoginWebViewController alloc]initWithUrl:kUserAgreementUrl];
        webVC.navigationItem.title = @"用户协议";
        RCDBaseNavigationController *nav = [[RCDBaseNavigationController alloc]initWithRootViewController:webVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:NO completion:NULL];
        
//        RCDWebViewController *webVC = [[RCDWebViewController alloc]initWithUrl:kPrivacyUrl];
//        webVC.navigationItem.title = @"用户协议";
//        [self.navigationController pushViewController:webVC animated:YES];
    }];
    
    [self.policyView setDidSelected:^(BOOL isSelected) {
       
        StrongSelf
        self.isSelectedPrivacyAcctont = isSelected;
    }];

}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDarkContent;
}

#pragma mark -- private

- (void)loginRequest
{
    [self.view endEditing:YES];
    
    if (IsEmpty(self.mobileView.model.content)) {
        [self showTextHUDWithMessage:@"请输入账号"];
        return;
    }
    
//    if (self.mobileView.model.content.length < 11) {
//        [self showTextHUDWithMessage:@"手机号格式不正确"];
//        return;
//    }
    
    if (IsEmpty(self.pswView.model.content)) {
        [self showTextHUDWithMessage:@"请输入密码"];
        return;
    }
    
    if (!self.isSelectedPrivacyAcctont) {
        [self showTextHUDWithMessage:@"请勾选《用户协议》和《隐私条款》"];
        return;
    }
    
    NSDictionary *params = @{
        @"login":self.mobileView.model.content,
        @"password":self.pswView.model.content
    };
    
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [LoginApis stuLoginRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        
        UserInfoModel *userModel = [UserInfoModel mj_objectWithKeyValues:responseData];
        [GVUserDefaults standardUserDefaults].class_name = userModel.class_name;
        [GVUserDefaults standardUserDefaults].class_id = userModel.class_id;
        [GVUserDefaults standardUserDefaults].id_card = userModel.id_card;
        [GVUserDefaults standardUserDefaults].organ_name = userModel.organ_name;
        [GVUserDefaults standardUserDefaults].organ_id = userModel.organ_id;
        [GVUserDefaults standardUserDefaults].password = userModel.password;
        [GVUserDefaults standardUserDefaults].status = userModel.status;
        [GVUserDefaults standardUserDefaults].student_id = userModel.student_id;
        [GVUserDefaults standardUserDefaults].student_name = userModel.student_name;
        [GVUserDefaults standardUserDefaults].gender = userModel.sex;
        [GVUserDefaults standardUserDefaults].isLoginSuccess = @(1);

    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];


}

-(void)pswBtnAction
{
    self.pswBtn.selected = !self.pswBtn.selected;
    self.pswView.textFeild.secureTextEntry = !self.pswBtn.selected;
}

//-(UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleDarkContent;
//}

#pragma mark -- setupUI

- (void)setupViews
{
    self.view.backgroundColor = UIColorFromHex(kColorWhite);
    self.bgScrollView.contentView.backgroundColor = UIColorFromHex(kColorWhite);
    [self.bgScrollView.contentView addSubview:self.tipsLabel1];
    [self.bgScrollView.contentView addSubview:self.mobileBgView];
    [self.mobileBgView addSubview:self.mobileView];
    
    [self.bgScrollView.contentView addSubview:self.pswBgView];
    [self.pswBgView addSubview:self.pswView];
    [self.pswBgView addSubview:self.pswBtn];
    
    [self.bgScrollView.contentView addSubview:self.policyView];
    [self.bgScrollView.contentView addSubview:self.loginBtn];
    
    
    [self.pswBtn addTarget:self action:@selector(pswBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    WeakSelf
    [self.loginBtn setClickBlock:^(UIButton *btn) {
        StrongSelf
        [self.view endEditing:YES];
        [self loginRequest];
        
    }];
}

- (void)setupLayout
{
    [self.tipsLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.bgScrollView.contentView).offset(60*kScreen_Ratio_H+KStatusBarHeight);
        make.left.equalTo(self.bgScrollView.contentView).offset(25);
        make.right.equalTo(self.bgScrollView.contentView).offset(-25);
        make.height.mas_equalTo(30);
    }];
    
    [self.mobileBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.tipsLabel1.mas_bottom).offset(25);
        make.left.equalTo(self.bgScrollView.contentView).offset(25);
        make.right.equalTo(self.bgScrollView.contentView).offset(-25);
        make.height.mas_equalTo(50);
    }];
    
    [self.mobileView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mobileBgView).offset(0);
        make.bottom.equalTo(self.mobileBgView).offset(-kOnePixel);
        make.left.equalTo(self.mobileBgView).offset(0).offset(-16);
        make.right.equalTo(self.mobileBgView).offset(0);
    }];
    
    [self.pswBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mobileBgView.mas_bottom).offset(15);
        make.left.equalTo(self.bgScrollView.contentView).offset(25);
        make.right.equalTo(self.bgScrollView.contentView).offset(-25);
        make.height.mas_equalTo(50);
    }];
    
    [self.pswView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.pswBgView).offset(0);
        make.bottom.equalTo(self.pswBgView).offset(-kOnePixel);
        make.left.equalTo(self.pswBgView).offset(0).offset(-16);
        make.right.equalTo(self.pswBtn.mas_left).offset(0);
    }];
    
    [self.pswBtn mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.bottom.equalTo(self.pswBgView).offset(0);
        make.width.mas_equalTo(30);
        make.right.equalTo(self.pswBgView).offset(-10);
    }];
    
    [self.policyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pswBtn.mas_bottom).offset(44);
        make.left.equalTo(self.bgScrollView.contentView).offset(25);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.policyView.mas_bottom).offset(10);
        make.left.equalTo(self.bgScrollView.contentView).offset(25);
        make.right.equalTo(self.bgScrollView.contentView).offset(-25);;
        make.height.mas_equalTo(40);
        make.bottom.equalTo(self.bgScrollView.contentView.mas_bottom).offset(-40 + kBottomOffset);
    }];

}


#pragma mark -- lazyload

-(UIView *)mobileBgView
{
    if (!_mobileBgView) {
        _mobileBgView = [UIView new];
        _mobileBgView.backgroundColor = UIColorFromHex(kColorWhite);
        [_mobileBgView addBottomLine];
    }
    return _mobileBgView;
}

-(RCDTextFeidView *)mobileView
{
    if (!_mobileView) {
        RCDTextFeidViewModel *model = [RCDTextFeidViewModel new];
        model.placeholder = @"请输入账号";
        model.leftImgPath = @"login_mobile";
//        model.limitNum = 11;
        model.keyboardType = UIKeyboardTypeNumberPad;
        model.contentFont = UIFontSize(15);
        _mobileView = [[RCDTextFeidView alloc]initWithModel:model];
    }
    return _mobileView;
}

-(UIView *)pswBgView
{
    if (!_pswBgView) {
        _pswBgView = [UIView new];
        _pswBgView.backgroundColor = UIColorFromHex(kColorWhite);
        [_pswBgView addBottomLine];
    }
    return _pswBgView;
}


-(RCDTextFeidView *)pswView
{
    if (!_pswView) {
        RCDTextFeidViewModel *model = [RCDTextFeidViewModel new];
        model.leftImgPath = @"login_psw";
        model.placeholder = @"请输入密码";
//        model.limitNum = 6;
        model.keyboardType = UIKeyboardTypeASCIICapable;
        model.contentFont = UIFontSize(15);
        _pswView = [[RCDTextFeidView alloc]initWithModel:model];
        _pswView.backgroundColor = UIColorFromHex(kColorWhite);
        _pswView.textFeild.secureTextEntry = YES;
    }
    return _pswView;
}

-(UIButton *)pswBtn
{
    if (!_pswBtn) {
        _pswBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pswBtn setImage:UIImageWithName(@"eyesclose") forState:UIControlStateNormal];
        [_pswBtn setImage:UIImageWithName(@"eyesopen") forState:UIControlStateSelected];
        
    }
    return _pswBtn;
}

-(UILabel *)tipsLabel
{
    if (!_tipsLabel) {
        _tipsLabel = [UILabel new];
        _tipsLabel.text = @"您好，";
        _tipsLabel.textColor = UIColorFromHex(0x000000);
        _tipsLabel.font = UIBoldFontSize(18);
    }
    return _tipsLabel;
}

-(UILabel *)tipsLabel1
{
    if (!_tipsLabel1) {
        _tipsLabel1 = [UILabel new];
        _tipsLabel1.text = @"欢迎登录教之源学生";
        _tipsLabel1.textColor = UIColorFromHex(0x333333);
        _tipsLabel1.font = UIFontSize(20);
    }
    return _tipsLabel1;
}

-(RCDCornerBtn *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [RCDCornerBtn new];
        [_loginBtn setTitleString:@"登录"];
        _loginBtn.cornerRadius = 20;
    }
    return _loginBtn;
}

-(AgreementAndPolicyView *)policyView
{
    if (!_policyView) {
        _policyView = [[AgreementAndPolicyView alloc]init];
    }
    return _policyView;
}
@end
 
